//
//  MainViewController.swift
//  WeiBo
//
//  Created by iris on 16/3/13.
//  Copyright © 2016年 陈真真. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    // MARK:- 懒加载属性
    private lazy var imageNames : [String] = ["tabbar_home", "tabbar_message_center", "", "tabbar_discover", "tabbar_profile"]
    private lazy var composeBtn : UIButton = UIButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
    
    // MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupComposeBtn()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        adjustItems()
    }
}

// MARK:- 设置UI
extension MainViewController {
    /// 调整tabBar的item
    private func adjustItems() {
        let count = tabBar.items!.count
        for i in 0..<count {
            // 1.取出item
            let item = tabBar.items![i]
            
            // 2.判断如果下标值为2,那么不可以和用户交互
            if i == 2 {
                item.enabled = false
                continue
            }
            
            //3.设置item的选中图片
            item.selectedImage = UIImage(named: imageNames[i] + "_highlighted")
        }
    }
    
    /// 添加发布按钮
    private func setupComposeBtn() {
        // 1.将按钮添加到tabBar中
        tabBar.addSubview(composeBtn)
        
        // 2.设置发布按钮的位置
        composeBtn.center = CGPoint(x: tabBar.bounds.width * 0.5, y: tabBar.bounds.height * 0.5)
        
        // 3.监听按钮的点击
        composeBtn.addTarget(self, action: "composeBtnClick", forControlEvents: .TouchUpInside)
        
    }
}

// MARK:- 按钮的点击事件
extension MainViewController {
    @objc private func composeBtnClick() {
        print("composeBtnClick")
    }
}